home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / make-367.lha / make-3.67 / file.c < prev    next >
C/C++ Source or Header  |  1993-05-22  |  13KB  |  528 lines

  1. /* Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993
  2.     Free Software Foundation, Inc.
  3. This file is part of GNU Make.
  4.  
  5. GNU Make is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. GNU Make is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with GNU Make; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. #include "make.h"
  20. #include "commands.h"
  21. #include "dep.h"
  22. #include "file.h"
  23. #include "variable.h"
  24.  
  25.  
  26. /* Hash table of files the makefile knows how to make.  */
  27.  
  28. #ifndef    FILE_BUCKETS
  29. #define FILE_BUCKETS    1007
  30. #endif
  31. static struct file *files[FILE_BUCKETS];
  32.  
  33. /* Number of files with the `intermediate' flag set.  */
  34.  
  35. unsigned int num_intermediates = 0;
  36.  
  37.  
  38. /* Access the hash table of all file records.
  39.    lookup_file  given a name, return the struct file * for that name,
  40.            or nil if there is none.
  41.    enter_file   similar, but create one if there is none.  */
  42.  
  43. struct file *
  44. lookup_file (name)
  45.      char *name;
  46. {
  47.   register struct file *f;
  48.   register char *n;
  49.   register unsigned int hashval;
  50.  
  51.   if (*name == '\0')
  52.     abort ();
  53.  
  54.   /* This is also done in parse_file_seq, so this is redundant
  55.      for names read from makefiles.  It is here for names passed
  56.      on the command line.  */
  57.   while (name[0] == '.' && name[1] == '/' && name[2] != '\0')
  58.     {
  59.       name += 2;
  60.       while (*name == '/')
  61.     /* Skip following slashes: ".//foo" is "foo", not "/foo".  */
  62.     ++name;
  63.     }
  64.  
  65.   if (*name == '\0')
  66.     /* It was all slashes after a dot.  */
  67.     name = "./";
  68.  
  69.   hashval = 0;
  70.   for (n = name; *n != '\0'; ++n)
  71.     HASH (hashval, *n);
  72.   hashval %= FILE_BUCKETS;
  73.  
  74.   for (f = files[hashval]; f != 0; f = f->next)
  75.     if (streq (f->name, name))
  76.       return f;
  77.   return 0;
  78. }
  79.  
  80. struct file *
  81. enter_file (name)
  82.      char *name;
  83. {
  84.   register struct file *f, *new;
  85.   register char *n;
  86.   register unsigned int hashval;
  87.  
  88.   if (*name == '\0')
  89.     abort ();
  90.  
  91.   /* This is also done in parse_file_seq, so this is redundant
  92.      for names read from makefiles.  It is here for names passed
  93.      on the command line.  */
  94.   while (name[0] == '.' && name[1] == '/' && name[2] != '\0')
  95.     {
  96.       name += 2;
  97.       while (*name == '/')
  98.     /* Skip following slashes: ".//foo" is "foo", not "/foo".  */
  99.     ++name;
  100.     }
  101.  
  102.   if (*name == '\0')
  103.     {
  104.       /* It was all slashes!  Move back to the dot and truncate
  105.      it after the first slash, so it becomes just "./".  */
  106.       do
  107.     --name;
  108.       while (name[0] != '.');
  109.       name[2] = '\0';
  110.     }
  111.  
  112.   hashval = 0;
  113.   for (n = name; *n != '\0'; ++n)
  114.     HASH (hashval, *n);
  115.   hashval %= FILE_BUCKETS;
  116.  
  117.   for (f = files[hashval]; f != 0; f = f->next)
  118.     if (streq (f->name, name))
  119.       break;
  120.  
  121.   if (f != 0 && !f->double_colon)
  122.     return f;
  123.  
  124.   new = (struct file *) xmalloc (sizeof (struct file));
  125.   bzero ((char *) new, sizeof (struct file));
  126.   new->name = name;
  127.   new->update_status = -1;
  128.  
  129.   if (f == 0)
  130.     {
  131.       /* This is a completely new file.  */
  132.       new->next = files[hashval];
  133.       files[hashval] = new;
  134.     }
  135.   else
  136.     {
  137.       /* There is already a double-colon entry for this file.  */
  138.       while (f->prev != 0)
  139.     f = f->prev;
  140.       f->prev = new;
  141.     }
  142.  
  143.   return new;
  144. }
  145.  
  146. /* Rename FILE to NAME.  This is not as simple as resetting
  147.    the `name' member, since it must be put in a new hash bucket,
  148.    and possibly merged with an existing file called NAME.  */
  149.  
  150. void
  151. rename_file (file, name)
  152.      register struct file *file;
  153.      char *name;
  154. {
  155.   char *oldname = file->name;
  156.   register unsigned int oldhash;
  157.   register char *n;
  158.  
  159.   while (file->renamed != 0)
  160.     file = file->renamed;
  161.  
  162.   /* Find the hash values of the old and new names.  */
  163.  
  164.   oldhash = 0;
  165.   for (n = oldname; *n != '\0'; ++n)
  166.     HASH (oldhash, *n);
  167.  
  168.   file_hash_enter (file, name, oldhash, file->name);
  169. }
  170.  
  171. void
  172. file_hash_enter (file, name, oldhash, oldname)
  173.      register struct file *file;
  174.      char *name;
  175.      unsigned int oldhash;
  176.      char *oldname;
  177. {
  178.   unsigned int oldbucket = oldhash % FILE_BUCKETS;
  179.   register unsigned int newhash, newbucket;
  180.   struct file *oldfile;
  181.   register char *n;
  182.   register struct file *f;
  183.  
  184.   newhash = 0;
  185.   for (n = name; *n != '\0'; ++n)
  186.     HASH (newhash, *n);
  187.   newbucket = newhash % FILE_BUCKETS;
  188.  
  189.   /* Look for an existing file under the new name.  */
  190.  
  191.   for (oldfile = files[newbucket]; oldfile != 0; oldfile = oldfile->next)
  192.     if (streq (oldfile->name, name))
  193.       break;
  194.  
  195.   if (oldhash != 0 && (newbucket != oldbucket || oldfile != 0))
  196.     {
  197.       /* Remove FILE from its hash bucket.  */
  198.  
  199.       struct file *lastf = 0;
  200.  
  201.       for (f = files[oldbucket]; f != file; f = f->next)
  202.     lastf = f;
  203.  
  204.       if (lastf == 0)
  205.     files[oldbucket] = f->next;
  206.       else
  207.     lastf->next = f->next;
  208.     }
  209.  
  210.   /* Give FILE its new name.  */
  211.  
  212.   for (f = file; f != 0; f = f->prev)
  213.     f->name = name;
  214.  
  215.   if (oldfile == 0)
  216.     {
  217.       /* There is no existing file with the new name.  */
  218.  
  219.       if (newbucket != oldbucket)
  220.     {
  221.       /* Put FILE in its new hash bucket.  */
  222.       file->next = files[newbucket];
  223.       files[newbucket] = file;
  224.     }
  225.     }
  226.   else
  227.     {
  228.       /* There is an existing file with the new name.
  229.      We must merge FILE into the existing file.  */
  230.  
  231.       register struct dep *d;
  232.  
  233.       if (file->cmds != 0)
  234.     {
  235.       if (oldfile->cmds == 0)
  236.         oldfile->cmds = file->cmds;
  237.       else if (file->cmds != oldfile->cmds)
  238.         {
  239.           /* We have two sets of commands.  We will go with the
  240.          one given in the rule explicitly mentioning this name,
  241.          but give a message to let the user know what's going on.  */
  242.           if (oldfile->cmds->filename != 0)
  243.         makefile_error (file->cmds->filename, file->cmds->lineno,
  244.                 "Commands were specified for \
  245. file `%s' at %s:%u,",
  246.                 oldname, oldfile->cmds->filename,
  247.                 oldfile->cmds->lineno);
  248.           else
  249.         makefile_error (file->cmds->filename, file->cmds->lineno,
  250.                 "Commands for file `%s' were found by \
  251. implicit rule search,",
  252.                 oldname);
  253.           makefile_error (file->cmds->filename, file->cmds->lineno,
  254.                   "but `%s' is now considered the same file \
  255. as `%s'.",
  256.                   oldname, name);
  257.           makefile_error (file->cmds->filename, file->cmds->lineno,
  258.                   "Commands for `%s' will be ignored \
  259. in favor of those for `%s'.",
  260.                   name, oldname);
  261.         }
  262.     }
  263.  
  264.       /* Merge the dependencies of the two files.  */
  265.  
  266.       d = oldfile->deps;
  267.       if (d == 0)
  268.     oldfile->deps = file->deps;
  269.       else
  270.     {
  271.       while (d->next != 0)
  272.         d = d->next;
  273.       d->next = file->deps;
  274.     }
  275.  
  276.       merge_variable_set_lists (&oldfile->variables, file->variables);
  277.  
  278.       if (oldfile->double_colon && !file->double_colon)
  279.     fatal ("can't rename single-colon `%s' to double-colon `%s'",
  280.            oldname, name);
  281.       if (!oldfile->double_colon && file->double_colon)
  282.     fatal ("can't rename double-colon `%s' to single-colon `%s'",
  283.            oldname, name);
  284.  
  285.       if (file->last_mtime > oldfile->last_mtime)
  286.     /* %%% Kludge so -W wins on a file that gets vpathized.  */
  287.     oldfile->last_mtime = file->last_mtime;
  288.  
  289. #define MERGE(field) oldfile->field |= file->field
  290.       MERGE (precious);
  291.       MERGE (tried_implicit);
  292.       MERGE (updating);
  293.       MERGE (updated);
  294.       MERGE (is_target);
  295.       MERGE (cmd_target);
  296.       MERGE (phony);
  297. #undef MERGE
  298.  
  299.       file->renamed = oldfile;
  300.     }
  301. }
  302.  
  303. /* Remove all nonprecious intermediate files.
  304.    If SIG is nonzero, this was caused by a fatal signal,
  305.    meaning that a different message will be printed, and
  306.    the message will go to stderr rather than stdout.  */
  307.  
  308. void
  309. remove_intermediates (sig)
  310.      int sig;
  311. {
  312.   register int i;
  313.   register struct file *f;
  314.   char doneany;
  315.   
  316.   if (!sig && just_print_flag)
  317.     return;
  318.  
  319.   doneany = 0;
  320.   for (i = 0; i < FILE_BUCKETS; ++i)
  321.     for (f = files[i]; f != 0; f = f->next)
  322.       if (f->intermediate && (f->dontcare || !f->precious))
  323.     {
  324.       int status;
  325.       if (just_print_flag)
  326.         status = 0;
  327.       else
  328.         {
  329.           status = unlink (f->name);
  330.           if (status < 0 && errno == ENOENT)
  331.         continue;
  332.         }
  333.       if (!f->dontcare)
  334.         {
  335.           if (sig)
  336.         error ("*** Deleting file `%s'", f->name);
  337.           else if (!silent_flag)
  338.         {
  339.           if (! doneany)
  340.             {
  341.               fputs ("rm ", stdout);
  342.               doneany = 1;
  343.             }
  344.           else
  345.             putchar (' ');
  346.           fputs (f->name, stdout);
  347.           fflush (stdout);
  348.         }
  349.           if (status < 0)
  350.         perror_with_name ("unlink: ", f->name);
  351.         }
  352.     }
  353.  
  354.   if (doneany && !sig)
  355.     {
  356.       putchar ('\n');
  357.       fflush (stdout);
  358.     }
  359. }
  360.  
  361. /* For each dependency of each file, make the `struct dep' point
  362.    at the appropriate `struct file' (which may have to be created).
  363.  
  364.    Also mark the files depended on by .PRECIOUS and .PHONY.  */
  365.  
  366. void
  367. snap_deps ()
  368. {
  369.   register struct file *f, *f2;
  370.   register struct dep *d;
  371.   register int i;
  372.  
  373.   /* Enter each dependency name as a file.  */
  374.   for (i = 0; i < FILE_BUCKETS; ++i)
  375.     for (f = files[i]; f != 0; f = f->next)
  376.       for (f2 = f; f2 != 0; f2 = f2->prev)
  377.     for (d = f2->deps; d != 0; d = d->next)
  378.       if (d->name != 0)
  379.         {
  380.           d->file = lookup_file (d->name);
  381.           if (d->file == 0)
  382.         d->file = enter_file (d->name);
  383.           else
  384.         free (d->name);
  385.           d->name = 0;
  386.         }
  387.   
  388.   for (f = lookup_file (".PRECIOUS"); f != 0; f = f->prev)
  389.     for (d = f->deps; d != 0; d = d->next)
  390.       for (f2 = d->file; f2 != 0; f2 = f2->prev)
  391.     f2->precious = 1;
  392.  
  393.   for (f = lookup_file (".PHONY"); f != 0; f = f->prev)
  394.     for (d = f->deps; d != 0; d = d->next)
  395.       for (f2 = d->file; f2 != 0; f2 = f2->prev)
  396.     {
  397.       /* Mark this file as phony and nonexistent.  */
  398.       f2->phony = 1;
  399.       f2->last_mtime = (time_t) -1;
  400.     }
  401.  
  402.   f = lookup_file (".EXPORT_ALL_VARIABLES");
  403.   if (f != 0 && f->is_target)
  404.     export_all_variables = 1;
  405. }
  406.  
  407. /* Print the data base of files.  */
  408.  
  409. void
  410. print_file_data_base ()
  411. {
  412.   register unsigned int i, nfiles, per_bucket;
  413.   register struct file *file;
  414.   register struct dep *d;
  415.  
  416.   puts ("\n# Files");
  417.  
  418.   per_bucket = nfiles = 0;
  419.   for (i = 0; i < FILE_BUCKETS; ++i)
  420.     {
  421.       register unsigned int this_bucket = 0;
  422.  
  423.       for (file = files[i]; file != 0; file = file->next)
  424.     {
  425.       register struct file *f;
  426.  
  427.       ++this_bucket;
  428.  
  429.       for (f = file; f != 0; f = f->prev)
  430.         {
  431.           putchar ('\n');
  432.           if (!f->is_target)
  433.         puts ("# Not a target:");
  434.           printf ("%s:%s", f->name, f->double_colon ? ":" : "");
  435.           
  436.           for (d = f->deps; d != 0; d = d->next)
  437.         printf (" %s", dep_name (d));
  438.           putchar ('\n');
  439.           
  440.           if (f->precious)
  441.         puts ("#  Precious file (dependency of .PRECIOUS).");
  442.           if (f->phony)
  443.         puts ("#  Phony target (dependency of .PHONY).");
  444.           if (f->cmd_target)
  445.         puts ("#  Command-line target.");
  446.           if (f->dontcare)
  447.         puts ("#  A default or MAKEFILES makefile.");
  448.           printf ("#  Implicit rule search has%s been done.\n",
  449.               f->tried_implicit ? "" : " not");
  450.           if (f->stem != 0)
  451.         printf ("#  Implicit/static pattern stem: `%s'\n", f->stem);
  452.           if (f->intermediate)
  453.         puts ("#  File is an intermediate dependency.");
  454.           if (f->also_make != 0)
  455.         {
  456.           fputs ("#  Also makes:", stdout);
  457.           for (d = f->also_make; d != 0; d = d->next)
  458.             printf (" %s", dep_name (d));
  459.           putchar ('\n');
  460.         }
  461.           if (f->last_mtime == (time_t) 0)
  462.         puts ("#  Modification time never checked.");
  463.           else if (f->last_mtime == (time_t) -1)
  464.         puts ("#  File does not exist.");
  465.           else
  466.         printf ("#  Last modified %.24s (%ld)\n",
  467.             ctime (&f->last_mtime), (long int) f->last_mtime);
  468.           printf ("#  File has%s been updated.\n",
  469.               f->updated ? "" : " not");
  470.           switch (f->command_state)
  471.         {
  472.         case cs_running:
  473.           puts ("#  Commands currently running (THIS IS A BUG).");
  474.           break;
  475.         case cs_deps_running:
  476.           puts ("#  Dependencies commands running (THIS IS A BUG).");
  477.           break;
  478.         case cs_not_started:
  479.         case cs_finished:
  480.           switch (f->update_status)
  481.             {
  482.             case -1:
  483.               break;
  484.             case 0:
  485.               puts ("#  Successfully updated.");
  486.               break;
  487.             case 1:
  488.               puts ("#  Failed to be updated.");
  489.               break;
  490.             default:
  491.               puts ("#  Invalid value in `update_status' member!");
  492.               fflush (stdout);
  493.               fflush (stderr);
  494.               abort ();
  495.             }
  496.           break;
  497.         default:
  498.           puts ("#  Invalid value in `command_state' member!");
  499.           fflush (stdout);
  500.           fflush (stderr);
  501.           abort ();
  502.         }
  503.  
  504.           if (f->variables != 0)
  505.         print_file_variables (file);
  506.  
  507.           if (f->cmds != 0)
  508.         print_commands (f->cmds);
  509.         }
  510.     }
  511.  
  512.       nfiles += this_bucket;
  513.       if (this_bucket > per_bucket)
  514.     per_bucket = this_bucket;
  515.     }
  516.  
  517.   if (nfiles == 0)
  518.     puts ("\n# No files.");
  519.   else
  520.     {
  521.       printf ("\n# %u files in %u hash buckets.\n", nfiles, FILE_BUCKETS);
  522. #ifndef    NO_FLOAT
  523.       printf ("# average %.1f files per bucket, max %u files in one bucket.\n",
  524.           ((double) FILE_BUCKETS) / ((double) nfiles) * 100.0, per_bucket);
  525. #endif
  526.     }
  527. }
  528.